package com.rc.saas.mini.common.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.xml.bind.DatatypeConverter;
import java.security.SecureRandom;

public class SecurityTool {

    private static final String DESCode="const::aqua";

    public static String decodeDES(String raw){
        try {
            SecureRandom random=new SecureRandom();
            DESKeySpec desKeySpec=new DESKeySpec(DESCode.getBytes("utf-8"));
            SecretKeyFactory secretKeyFactory=SecretKeyFactory.getInstance("DES");
            SecretKey secretKey=secretKeyFactory.generateSecret(desKeySpec);
            Cipher cipher=Cipher.getInstance("DES");
            cipher.init(Cipher.DECRYPT_MODE,secretKey,random);
            return new String(cipher.doFinal(DatatypeConverter.parseBase64Binary(raw)),"utf-8");
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    private SecurityTool(){}
}
